//
//  ManagerAlumViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/31.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "ManagerAlumViewController.h"
#import "MyPictureVC.h"
@interface ManagerAlumViewController ()<UIAlertViewDelegate>

@end

@implementation ManagerAlumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
   // [self addBackItem];
    self.navigationItem.title = @"编辑相册";
    //[self addNavgationBar];
    [self addRightItem];
    // Do any additional setup after loading the view.
}
-(void)main
{
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel * nameLab = [[UILabel alloc]initWithFrame:CGRectMake(SIZETO6W(15), 64+SIZETO6H(20), 100, 30)];
    nameLab.text = @"修改名称：";
    [self.view addSubview:nameLab];
    
    UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(nameLab.right, nameLab.top, SCREEN_WIDTH-nameLab.right-SIZETO6W(15), 30)];
    textField.tag = 712;
    textField.text = _alumName;
    [self.view addSubview:textField];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(SIZETO6W(30), SCREEN_HEIGHT/2, SCREEN_WIDTH-SIZETO6W(30)*2, 45);
    [btn setTitle:@"删除" forState:UIControlStateNormal];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 5;
    btn.layer.borderColor = [[UIColor redColor] CGColor];
    btn.layer.borderWidth = 1.0f;
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}
-(void)modifyAlumwithtype:(NSString *)type
{
    UITextField * textField = (UITextField *)[self.view viewWithTag:712];
    __weak typeof(self)weakSelf = self;
    [U76HONet modifyAlumWitnAction:@"" code:[Singleton share].code type:type pid:_pid name:textField.text andDataBlock:^(NSDictionary *dic) {
        NSLog(@"编辑相册数据成功列");
        if ([type isEqualToString:@"1"]) {
            for (UIViewController *temp in weakSelf.navigationController.viewControllers) {
                if ([temp isKindOfClass:[MyPictureVC class]]) {
                    [weakSelf.navigationController popToViewController:temp animated:YES];
                }
            }
        }
        else
        {
            UITextField * textField = (UITextField *)[weakSelf.view viewWithTag:712];
            [weakSelf.delegate changeAlumNameWithnae:textField.text];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];
}
-(void)btnAction:(UIButton *)btn
{
    NSLog(@"删除被点击了");
    if ([self isLogin]) {
        [self modifyAlumwithtype:@"1"];
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.editing = YES;
}
#pragma mark - addrightItem
-(void)addRightItem
{
    UIButton * finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    finishBtn.frame = CGRectMake(0, 0, 40, 40);
    [finishBtn setTitle:@"完成" forState:UIControlStateNormal];
    [finishBtn setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
    [finishBtn addTarget:self action:@selector(finishAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rigthtItem = [[UIBarButtonItem alloc]initWithCustomView:finishBtn];
    self.navigationItem.rightBarButtonItem = rigthtItem;
}
-(void)finishAction:(UIButton *)btn
{
    NSLog(@"完成按钮被点击了");
    if ([self isLogin]) {
        [self modifyAlumwithtype:@"2"];
    }
}
-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
                nvc.navigationBarHidden = YES;
                
                [self presentViewController:nvc animated:YES completion:nil];
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
        nvc.navigationBarHidden = YES;
        
        [self presentViewController:nvc animated:YES completion:nil];    }
}

#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
